package com.adams.app.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.adams.app.R;
import com.adams.app.data.RequestManager;
import com.adams.app.vendor.HappyGyApi;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;

public class ImgItemsAdapter extends ArrayAdapter<String> {

	Context context;
	LayoutInflater inflater;
	int layoutResourceId;
	float imageWidth;
	private Drawable mDefaultImageDrawable = new ColorDrawable(Color.argb(255,
			201, 201, 201));

	public ImgItemsAdapter(Context context, int layoutResourceId, String[] items) {
		super(context, layoutResourceId, items);
		this.context = context;
		this.layoutResourceId = layoutResourceId;

		float width = ((Activity) context).getWindowManager()
				.getDefaultDisplay().getWidth();
		float margin = (int) convertDpToPixel(10f, (Activity) context);
		// two images, three margins of 10dips
		imageWidth = ((width - (3 * margin)) / 2);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FrameLayout row = (FrameLayout) convertView;
		ItemHolder holder;
		String url = getItem(position);

		if (row == null) {
			holder = new ItemHolder();
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = (FrameLayout) inflater.inflate(layoutResourceId, parent,
					false);
			ImageView itemImage = (ImageView) row.findViewById(R.id.item_image);
			holder.itemImage = itemImage;
		} else {
			holder = (ItemHolder) row.getTag();
		}

		row.setTag(holder);
		setImageBitmap(HappyGyApi.BASE_URL + url, holder.itemImage);
		return row;
	}

	public static class ItemHolder {
		ImageView itemImage;
	}

	// resize the image proportionately so it fits the entire space
	private void setImageBitmap(String url, final ImageView imageView) {
		RequestManager.loadImage(url, new ImageListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				if (error != null) {
					imageView.setImageDrawable(mDefaultImageDrawable);
				}
			}

			@Override
			public void onResponse(ImageContainer response, boolean isImmediate) {
				if (response.getBitmap() != null) {
					if (!isImmediate && mDefaultImageDrawable != null) {

						Bitmap bitmap = response.getBitmap();
						float i = ((float) imageWidth)
								/ ((float) bitmap.getWidth());
						float imageHeight = i * (bitmap.getHeight());
						FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView
								.getLayoutParams();
						params.height = (int) imageHeight;
						params.width = (int) imageWidth;
						imageView.setLayoutParams(params);
						imageView.setImageBitmap(bitmap);
						// TransitionDrawable transitionDrawable = new
						// TransitionDrawable(
						// new Drawable[] {
						// mDefaultImageDrawable,
						// new BitmapDrawable(AppData.getContext()
						// .getResources(), bitmap) });
						// transitionDrawable.setCrossFadeEnabled(true);
						// imageView.setImageDrawable(transitionDrawable);
						// transitionDrawable.startTransition(100);
					} else {
						imageView.setImageBitmap(response.getBitmap());
					}
				} else if (mDefaultImageDrawable != null) {
					imageView.setImageDrawable(mDefaultImageDrawable);
				}
			}
		});
	}

	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}

}